package com.example.java_gobang.game;

import java.util.ArrayList;
import java.util.List;

public class AIUser {
    private static final int SIZE = 15;
    private static final int EMPTY = 0;
    private static final int PLAYER = 1;
    private static final int ROBOT = 2;
    private int[][] board = new int[SIZE][SIZE];
    public AIUser(int[][] board) {
        copyBoard(board);
    }
    private void copyBoard(int[][] source) {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                this.board[i][j] = source[i][j];
            }
        }
    }

    public  int[] getNextMove() {
        int[] bestMove = null;
        int bestScore = Integer.MIN_VALUE;

        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                if (board[i][j] == EMPTY) {
                    board[i][j] = ROBOT;
                    int score = evaluateBoard(board);
                    board[i][j] = EMPTY;
                    if (score > bestScore) {
                        bestScore = score;
                        bestMove = new int[]{i, j};
                    }
                }
            }
        }

        return bestMove;
    }

    private static int evaluateBoard(int[][] board) {
        int score = 0;
        score += evaluateLines(board, ROBOT);
        score -= evaluateLines(board, PLAYER);
        return score;
    }

    private static int evaluateLines(int[][] board, int player) {
        int score = 0;

        // 水平、垂直、和对角线方向的评分
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                if (board[i][j] == player) {
                    score += evaluateDirection(board, player, i, j, 1, 0); // 水平
                    score += evaluateDirection(board, player, i, j, 0, 1); // 垂直
                    score += evaluateDirection(board, player, i, j, 1, 1); // 对角线 \
                    score += evaluateDirection(board, player, i, j, 1, -1); // 对角线 /
                }
            }
        }
        return score;
    }

    private static int evaluateDirection(int[][] board, int player, int x, int y, int dx, int dy) {
        int count = 0;
        int blocks = 0;
        int score = 0;

        for (int i = 0; i < 5; i++) {
            int nx = x + i * dx;
            int ny = y + i * dy;
            if (nx >= 0 && nx < SIZE && ny >= 0 && ny < SIZE) {
                if (board[nx][ny] == player) {
                    count++;
                } else if (board[nx][ny] != EMPTY) {
                    blocks++;
                    break;
                }
            }
        }

        if (blocks == 0) {
            switch (count) {
                case 5:
                    score = 10000;
                    break;
                case 4:
                    score = 1000;
                    break;
                case 3:
                    score = 100;
                    break;
                case 2:
                    score = 10;
                    break;
                case 1:
                    score = 1;
                    break;
            }
        }

        return score;
    }
}
